/*
 * Copyright (C) 2001-2005 Pleasant nightmare studio
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.pleasantnightmare.noraxidium.collision;

import org.apache.log4j.Logger;
import org.lwjgl.util.Rectangle;
import org.pleasantnightmare.noraxidium.math.GameVector2f;

/**
 * Collision bound in form of a rectangle.
 *
 * @author deus
 * @version 1.0
 * @since Jul 9, 2010 2:12:13 PM
 */
public class RectBound implements CollisionBound {
  private static final Logger LOGGER = Logger.getLogger(RectBound.class);

  private Rectangle rect;
  private CollisionType collisionType;

  public RectBound(Rectangle rect, CollisionType collisionType) {
    if (rect == null)
      throw new IllegalArgumentException("rect = null");
    if (collisionType == null)
      throw new IllegalArgumentException("collisionType = null");
    this.rect = rect;
    this.collisionType = collisionType;
  }

  public Rectangle getRect() {
    return rect;
  }

  public void setPosition(GameVector2f position) {
    rect.setX((int) position.getX());
    rect.setY((int) position.getY());
  }

  public void setWidth(int width) {
    rect.setWidth(width);
  }

  public void setHeight(int height) {
    rect.setHeight(height);
  }

  @Override public CollisionResult calculateAgainst(RectBound rectBound) {
    CollisionResult result = new CollisionResult();

    int left1 = rect.getX();
    int right1 = rect.getX() + rect.getWidth();
    int top1 = rect.getY();
    int bottom1 = rect.getY() + rect.getHeight();

    int left2 = rectBound.getRect().getX();
    int top2 = rectBound.getRect().getY();
    int right2 = rectBound.getRect().getX() + rectBound.getRect().getWidth();
    int bottom2 = rectBound.getRect().getY() + rectBound.getRect().getHeight();

    int commonWidth = rect.getWidth() + rectBound.getRect().getWidth();
    int commonHeight = rect.getHeight() + rectBound.getRect().getHeight();

    int exitX = Math.max(0, left1 - right2) + Math.min(0, left2 - right1);
    int exitY = Math.max(0, top1 - bottom2) + Math.min(0, top2 - bottom1);

    result.addBound(this, new GameVector2f((float) exitX, (float) exitY));

    return result;
  }

/*
  @Override public CollisionResult calculateAgainst(RectBound rectBound) {
    Rectangle intersection = new Rectangle();
    rect.intersection(rectBound.getRect(), intersection);

    CollisionResult result = new CollisionResult();
    result.setCollisionDetected(intersection.isEmpty());
    if (result.isCollisionDetected()) {
      GameVector2f exitVector = new GameVector2f();

      float localWidth = Math.abs(intersection.getWidth()) - intersection.getX();
      float localHeight = Math.abs(intersection.getHeight()) - intersection.getY();

      if (localWidth > localHeight)
        exitVector.setX(-localWidth);
      else
        exitVector.setY(-localHeight);

      result.addBound(this, exitVector);
    }

    return result;
  }
*/

  @Override public CollisionType getCollisionType() {
    return collisionType;
  }
}
